/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.dao.hibernate;

import it.blueocean.acanto.dao.MenuItemDao;
import it.blueocean.acanto.dao.common.AGenericDaoHibernate;
import it.blueocean.acanto.model.MenuItem;
import it.blueocean.acanto.model.NavigationItem;

import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.inject.Named;

@Named("menuItemDao")
public class MenuItemDaoHibernate extends AGenericDaoHibernate<MenuItem, Long> implements MenuItemDao
{
	public SortedSet<MenuItem> getOrderedMenuForNavigationItem(NavigationItem navigationItem)
	{
		List<MenuItem> listMenuItems = findByQuery("from MenuItem menu where menu.parent = ?", navigationItem.getTitle());

		SortedSet<MenuItem> menu = sortedMenuItem(listMenuItems);

		return menu;
	}
	
	/**
	 * Convert an ordinary list in a sorted Set
	 * 
	 * @param listMenuItems
	 * @return SortedSet<MenuItem>
	 */
	private SortedSet<MenuItem> sortedMenuItem(List<MenuItem> listMenuItems)
	{
		SortedSet<MenuItem> sortedMenuItem = new TreeSet<MenuItem>();
		sortedMenuItem.addAll(listMenuItems);
		
		return sortedMenuItem;
	}
}
